package http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class HttpServletRequest {

    private String method;
    private String requsetURL;
    private String protocol;

    //头域参数容器
    private Map<String,String> headerMap = new LinkedHashMap<>();
    //地址内参数容器
    private Map<String,String> paramsMap = new LinkedHashMap<>();

    public HttpServletRequest(InputStream in) throws IOException {

        //将字节流转换成字符流
        InputStreamReader isr = new InputStreamReader(in);
        //使用字符缓冲流
        BufferedReader br = new BufferedReader(isr);
        String line;
        int rowNumber = 0;
        while((line = br.readLine()) != null){
            //输出请求头行 和头域
            System.out.println(line);
            if (rowNumber == 0){
                String items[] = line.split(" ");
                method = items[0]; //请求方法
                requsetURL = items[1]; //请求地址
                protocol = items[2]; //请求协议版本

                //请求参数解析
                if(requsetURL.contains("?")){
                    String its[] = requsetURL.split("\\?");
                    requsetURL = its[0];
                    //解析请求地址中的参数
                    its = its[1].split("&");
                    for(String it : its){
                        String[] is = it.split("=");
                        paramsMap.put(is[0], is[1]);
                    }
                }
            }else if(line.isEmpty()){
                //暂时不处理请求体
                break;
            }else {
                //头域解析
                int index = line.indexOf(": ");
                String items[] = {null,null};
                items[0] = line.substring(0,index);
                items[1] = line.substring(index+2);
                headerMap.put(items[0], items[1]); //将头域参数保存到 容器中
            }
            rowNumber++;
        }
    }

    public String getMethod(){
        return method;
    }

    public String getRequestURI(){
        return requsetURL;
    }

    public String getProtocol(){
        return protocol;
    }

    //头域参数
    public String  getHeader(String name){
        return headerMap.get(name);
    }

    //所有头域参数名
    public Collection<String> getHeaderNames(){
        return headerMap.keySet();
    }

    //获取头行中的请求参数
    public String getParameter(String name){
        return paramsMap.get(name);
    }


    public Cookie[] getCookies(){
        String cookieStr = headerMap.get("Cookie");  //从头域中获取cookie字段的值
        if(cookieStr != null){
            String[] items = cookieStr.split(";\\s*"); //用分号将每一组cookie对象分开
            Cookie[] cookies = new Cookie[items.length];
            for(int i = 0; i < items.length; i++){
                String item = items[i];
                String name = item.replaceAll("(.+)=.+","$1");
                String value = item.replaceAll(".+=(.+)", "$1");

                //将每一个键值对创建成cookie对像
                cookies[i] = new Cookie(name,value);
            }
            return cookies;
        }else{
            return null;
        }
    }

    //设置请求中的会话
    private  HttpSession session;
    public HttpSession getSssion(){
        return session;
    }

    //设置会话
    void setSession(HttpSession session){
        this.session = session;
    }
}
